$(function(){
	layui.use(['layer', 'form'], function() {
		form = layui.form();
		init();
	})
});
function init(){
	getArea(1,1);
	getBuyerInfo();
	$('.btn').bind('click',function(){
		submitConsMsg();
	});
}
var getBuyerInfo = function(){
var userId = userid;
	
	$.ajax({
		url:server_url+"rest/buyerOrder/getBuyerInfo.do?userId="+userId+"&ran="+Math.random(),
		type:'get',
		async:false,
		error:function(){alert(contants.systemError);},
		success:function(msg){
			var bur = msg.data;
			localStorage.setItem("buyerInfo", JSON.stringify(bur));
			console.log(bur);
			if(null != bur){
				$(document.body).append('<input type="hidden" id="buyerId" value="'+bur.buyerId+'" />');
				if(bur.consigneeList != null &&bur.consigneeList.length>0){
					$(document.body).append('<input type="hidden" id="consId" value="'+bur.consigneeList[0].id+'" />');
					$('#consName').val(bur.consigneeList[0].consigneer);
					$('#consMobile').val(bur.consigneeList[0].mobile);
					$('#consProvince').val(bur.consigneeList[0].provinceName);
					$('#consCity').val(bur.consigneeList[0].cityName);
					$('#consCounty').val(bur.consigneeList[0].countyName);
					$('#addr').val(bur.consigneeList[0].consigneerAddress)
					$("#consProvince").val(bur.consigneeList[0].province)
					getArea(bur.consigneeList[0].province,2)

					$("#consCity1").val(bur.consigneeList[0].city)
					$("#consCity").val(bur.consigneeList[0].city)
					getArea(bur.consigneeList[0].city,3)
					$("#consCounty1").val(bur.consigneeList[0].county)
					$("#consCounty").val(bur.consigneeList[0].county)
					form.render("select");
				}
			}
		}
	});
}
var form ;
var getArea = function(parent,servel){
	$.ajax({
		url:server_url+'rest/areaManager/getByParent.do?parentId='+parent,
		data:{ran:Math.random()},
		type:'get',
		async:false,
		error:function(){alert(contants.systemError);},
		success:function(msg){
			//console.log(msg);
			var areas = msg.data;
			var s = '';
			for(var i=0;i<areas.length;i++){
				s +='<option value="'+areas[i].areaId+'">'+areas[i].areaName+'</option>';
			}
			if(servel ==1){
				s= '<option value="">请选择省</option>' +s;
				$("#consProvince").html(s);
				form.render("select");
				form.on('select(consProvince)', function(data){
					var pro = data.value;
					$("#consProvince").val(pro);
					getArea(pro,2);
				});
			}else if(servel == 2){
				s= '<option value="">请选择市</option>' +s;
				$("#consCity1").html(s);
				form.render("select");
				form.on('select(consCity)', function(data){
					$("#consCity").val(data.value);
					console.log($('#consCity').val())
					getArea(data.value,3)
				});
			}else if(servel == 3){
				s= '<option value="">请选择县</option>' +s;
				$("#consCounty1").html(s);
				form.render("select");
				form.on('select(consCounty)', function(data){
					console.log(data.value)
					var pro = data.value;
					$("#consCounty").val(pro);
				});
			}

		}
		
	})
}
var submitConsMsg = function(){
	var jsonParam=[];
	if($('#consId').val() != undefined){
		jsonParam.push({name:'id',value:$('#consId').val()});
	}
	if($('#buyerId').val() != undefined){
		jsonParam.push({name:'buyerId',value:$('#buyerId').val()});
	}
	$('#consId').remove();
	$('#buyerId').remove();
	if($('#consName').val() != null &&$('#consName').val() !=''){
		var reg = /^([\u4e00-\u9fa5]+|([a-zA-Z]+\s?)+)$/;
		if($('#consName').val().trim().length<2|| $('#consName').val().trim().length>20){
			alert('输入字符须为2-20个中文或英文');return;
		}
		if(!reg.test($('#consName').val())){alert('姓名不规范');return;}
		jsonParam.push({name:'consigneer',value:$('#consName').val()});
	}else{
		alert('信息填写不完整,请检查');return;
	}
	if($('#consProvince').val() != null &&$('#consProvince').val() != ''){
		jsonParam.push({name:'province',value:$('#consProvince').val()});
	}else{
		alert('信息填写不完整,请检查');return;
	}
	if($('#consCity').val() != null && $('#consCity').val() != ''){
		jsonParam.push({name:'city',value:$('#consCity').val()});
	}else{
		alert('信息填写不完整,请检查');return;
	}
	if($('#consCounty').val() != null &&$('#consCounty').val() != ''){
		jsonParam.push({name:'county',value:$('#consCounty').val()});
	}else{
		alert('信息填写不完整,请检查');return;
	}
	if($('#addr').val() != null && $('#addr').val()!= ''){
		jsonParam.push({name:'consigneerAddress',value:$('#addr').val()});
	}else{
		alert('信息填写不完整,请检查');return;
	}
	if($('#consMobile').val() != null && $('#consMobile').val() != ''){
		var reg = /^[1][3578][0-9]{9}$/;
		if(reg.test($('#consMobile').val())){
			jsonParam.push({name:'mobile',value:$('#consMobile').val()});
		}else{
			alert('请输入正确的手机号');return;
		}
		
	}else{
		alert('信息填写不完整,请检查');return;
	}
	jsonParam.push({name:'addUser',value:localStorage.getItem("username")});
	//console.log(jsonParam);
	$.ajax({
		url:server_url+'buyer/product/rest/editConsiAddr.do?random='+Math.random(),
		type:'post',
		data:jsonParam,
		error:function(){alert(contants.systemError);},
		success:function(msg){
			//console.log(msg);
			var userId = userid
			
			$.ajax({
				url:server_url+"rest/buyerOrder/getBuyerInfo.do?userId="+userId,
				type:'get',
				data:{ran:Math.random()},
				ansyc:false,
				error:function(){alert(contants.systemError);},
				success:function(msg){
					var bur = msg.data;
					localStorage.setItem("buyerInfo", JSON.stringify(bur));
				}
			});
			goPerson();
			//localStorage.removeItem("buyerInfo");
		}
	});
}
var goPerson = function(){
	var url = params()['url'];
	window.location.href=url;
}